package com.enlorenz.core.channel.common.msgutil;

import java.util.LinkedHashMap;
import java.util.Map;

import com.enlorenz.common.util.CodeUtils;
import com.enlorenz.core.cache.imp.map.BaseMapCache;

/**
 * 字节消息解包
 * @author lcb
 */
public class ByteMsgParser {
	private byte [] buff;
	//解析序列
	private int index=0;
	//map数据
	private Map<String,Object> mapData=new LinkedHashMap<String, Object>();
	public ByteMsgParser(byte [] buff){
		this.buff=buff;
	}
	//获取bytes数组 工具
	public byte [] getArr(int len){
		byte [] data=new byte[len];
		System.arraycopy(buff, index, data, 0, len);
		index+=len;
		return data;
	}
	
	/**
	 * 翻转byte
	 * @param len
	 * @return
	 */
	private byte [] getArrReverse(int len){
		byte [] data=new byte[len];
		System.arraycopy(buff, index, data, 0, len);
		
		for(int i=index+len-1,j=0;i>=index;i--,j++){
			data[j]=buff[i];
		}
		
		index+=len;
		return data;
	}
	
	/**
	 * 获取bytes数组
	 * @param key
	 * @param len
	 */
	public void putBytesArr(String key,int len){
		mapData.put(key, getArr(len));
	}
	/**
	 * 将16进制转为10进制
	 * @param key
	 * @param len
	 */
	public void putNumHex2Ten(String key,int len){
		byte [] arr=getArr(len);
		Integer data=Integer.valueOf(CodeUtils.bytes2HexString(arr), 16);
		mapData.put(key,data );
	}
	
	/**
	 * 将16进制转为10进制
	 * @param key
	 * @param len
	 */
	public void putNumHex2TenPrint(String key,int len){
		byte [] arr=getArr(len);
		Long data=Long.valueOf(CodeUtils.bytes2HexString(arr), 16);
		putKeyValue(key, data);
	}
	/**
	 * 获取将16进制转为10进制
	 * @param key
	 * @param len
	 */
	public Long getNumHex2TenValue(int len){
		byte [] arr=getArr(len);
		Long data=Long.valueOf(CodeUtils.bytes2HexString(arr), 16);
		return data;
	}
	
	public Long getNumHex2TenValueReverseTrimAll(int len){
		byte [] arr=getArr(len);
		Long data=Long.valueOf(CodeUtils.bytes2HexString(CodeUtils.getTrimArr(arr)), 16);
		return data;
	}
	
	public void putNumHex2TenValueReverseTrimAll(String key,int len){
		putKeyValue(key, getNumHex2TenValueReverseTrimAll(len));
	}
	
	
	public void putNumHex2TenPrintReverse(String key,int len){
		byte [] arr=getArrReverse(len);
		Long data=Long.valueOf(CodeUtils.bytes2HexString(arr), 16);
		putKeyValue(key, data);
	}
	
	public Long getNumHex2TenValueReverse(int len){
		byte [] arr=getArrReverse(len);
		Long data=Long.valueOf(CodeUtils.bytes2HexString(arr), 16);
		return data;
	}
	
	public void putKeyValue(String key,Object value){
		print(key,value.toString());
		mapData.put(key,value );
	}
	
	public void putBytesArrPrint(String key,int len){
		byte [] buff=getArr(len);
		put2print(key, buff);
	}
	
	private void put2print(String key,byte [] buff){
		print(key,CodeUtils.bytes2HexString(buff));
		mapData.put(key,buff );
	}
	
	public void putString(String key,int len){
		mapData.put(key, new String(getArr(len)).trim());
	}
	
	public String getString(int len){
		return new String(getArr(len)).trim();
	}
	
	public void putString(String key,int len,String code) throws Exception{
		mapData.put(key, new String(getArr(len),code).trim());
	}
	
	public void putStringPrint(String key,int len,String code) throws Exception{
		String str=new String(getArr(len),code).trim();
		mapData.put(key, str);
		print(key,str);
	}
	
	public void putStringPrint(String key,int len) throws Exception{
		String str=new String(getArr(len)).trim();
		mapData.put(key, str);
		print(key,str);
	}
	
	protected void print(String key,String value){
		//System.out.println(key+":"+value);
	}
	
	/**
	 * 获取内部数据
	 * @return
	 */
	public Map<String,Object> getMapData(){
		return mapData;
	}
	
	/**
	 * 获取默认map缓存数据
	 * @return
	 */
	public BaseMapCache getDefaultMapData(){
		BaseMapCache d= new BaseMapCache();
		d.setDataStruts(mapData);
		return d;
	}
	
	public static void main(String[] args) throws Exception {
		String code="2a2ad500000000005310000001000000cccccccc0000000038363137383530303833343236393700cfe641343733323900000000000000000000000000000000000000000000000000000000000000004a705a430f56e110dc070c0c0d11060000000000dc070c0c1511110001a801000000000000000000000000000000000000000000000000000000000000000000000030303132303030303232303000303030300037323334323333360000000000000000000000000000000000000000000000000000000000000000001e00000000002323";
		System.out.println(code);
		ByteMsgParser bmp=new ByteMsgParser(
				CodeUtils.hexString2Bytes(code));
		/**
		 * 1024
		 */
		bmp.putBytesArrPrint("head", 2);
		bmp.putBytesArrPrint("length", 4);
		bmp.putBytesArrPrint("leave", 2);
		bmp.putBytesArrPrint("command", 4);
		bmp.putBytesArrPrint("dwCode", 4);
		bmp.putBytesArrPrint("cmdType", 4);
		//编号 UNIT
		bmp.putBytesArrPrint("unOrderId", 4);
		bmp.putBytesArrPrint("termId", 16);
		bmp.putStringPrint("szCarNum", 20);
		bmp.putBytesArrPrint("disp", 20);
		bmp.putBytesArrPrint("lon", 4);
		bmp.putBytesArrPrint("lat", 4);
		
		bmp.putBytesArrPrint("unYear", 2);
		bmp.putBytesArrPrint("btMonth", 1);
		bmp.putBytesArrPrint("btDay", 1);
		bmp.putBytesArrPrint("btHour", 1);
		bmp.putBytesArrPrint("btMin", 1);
		bmp.putBytesArrPrint("btSecond", 1);
		
		bmp.putBytesArrPrint("btWeek", 1);
		
		bmp.putBytesArrPrint("unDirect", 2);
		bmp.putBytesArrPrint("unSpeed", 2);
		
		bmp.putBytesArrPrint("unYear", 2);
		bmp.putBytesArrPrint("btMonth", 1);
		bmp.putBytesArrPrint("btDay", 1);
		bmp.putBytesArrPrint("btHour", 1);
		bmp.putBytesArrPrint("btMin", 1);
		bmp.putBytesArrPrint("btSecond", 1);
		
		
		bmp.putBytesArrPrint("btWeek", 1);
		
		bmp.putBytesArrPrint("valid", 1);
		
		bmp.putBytesArrPrint("voltage", 4);
		bmp.putBytesArrPrint("removes", 1);
		bmp.putBytesArrPrint("removeTime",20);
		
		bmp.putBytesArrPrint("szLabel",12);
		bmp.putBytesArrPrint("szValveStatus",13);
		bmp.putBytesArrPrint("szTermStatus",5);
		bmp.putBytesArrPrint("szEquip",21);
		bmp.putBytesArrPrint("szCardNo",20);
		bmp.putBytesArrPrint("nTermGroupId",4);
		bmp.putBytesArrPrint("validate",2);
		bmp.putBytesArrPrint("tail",2);
	}
}
